package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.dm.AppModuleParam;
import com.irdstudio.apicenter.console.service.appframe.AppModuleParamService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appModuleParam")
public class AppModuleParamAction {
	@Autowired
	private AppModuleParamService appModuleParamService;

	@RequestMapping(value = "appModuleParamDetail")
	public ModelAndView queryWithKeys(HttpServletRequest request)
			throws Exception {
		String appid = request.getParameter("appid");
		String mid = request.getParameter("moduleid");
		String paramCode = request.getParameter("paramCode");
		AppModuleParam appModuleParam = appModuleParamService
				.queryAppModuleParam(appid, mid, paramCode);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("appModuleParam", appModuleParam);
		modelAndView.setViewName("appframe/module_params");
		return modelAndView;
	}

	@RequestMapping(value = "updateAppModuleParam")
	public void updateAppFrameStart(
			@ModelAttribute("appModuleParam") AppModuleParam appModuleParam,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		String app_id = request.getParameter("appid");
		String module_id = request.getParameter("moduleid");
		String param_code = request.getParameter("paramcode");
		String param_value = request.getParameter("paramvalue");
		int flag = appModuleParamService.updateModuleParam(app_id, module_id,
				param_code, param_value);
		// int flag = appModuleParamService.updateModuleParam(appModuleParam);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}

		/*
		 * modelAndView.addObject("appModuleParam", appModuleParam);
		 * modelAndView.addObject("result",flag);
		 * modelAndView.setViewName("appframe/module_params"); return
		 * modelAndView;
		 */
	}

	@RequestMapping(value = "getAppModuleParamList")
	public ModelAndView getAppModuleParamList(
			@ModelAttribute("appModuleParam") AppModuleParam appModuleParam,
			HttpServletRequest request) throws Exception {
		String appid = request.getParameter("appid");
		String mid = request.getParameter("moduleid");
		ModelAndView modelAndView = new ModelAndView();
		List<AppModuleParam> paramList = appModuleParamService
				.queryAppModuleParamList(appid, mid);
		modelAndView.addObject("paramList", paramList);
		modelAndView.setViewName("appframe/moduleParamsList");
		return modelAndView;
	}

	@RequestMapping(value = "setModuleParameters")
	public ModelAndView deleteAppFrameModule(HttpServletRequest request)
			throws Exception {
		String appid = request.getParameter("appid");
		String mid = request.getParameter("moduleid");
		String paramCode = request.getParameter("paramCode");
		AppModuleParam appModuleParam = appModuleParamService
				.queryAppModuleParam(appid, mid, paramCode);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("appModuleParam", appModuleParam);
		modelAndView.setViewName("appframe/module_params");
		return modelAndView;
	}

}
